'''
Created on Oct 21, 2014

@author: arthurchang
'''
import os
import re
import time

from bs4 import BeautifulSoup


class ComparisonLibrary:
    result_folder = "CompareResult"
    currentpath = os.path.abspath(os.path.join(os.path.dirname('settings.py'),os.path.pardir))
    all_comparation_results = "AllComparationResults.txt"
#     currentpath = os.getcwd()
    
    def _compare_files(self, orifile, newfile, resultfile):
        if os.path.exists(resultfile):
            os.remove(resultfile)
        os.system("diff -b -B -E  %s %s >> %s" %(orifile, newfile, resultfile))
        filesize = os.path.getsize(resultfile)
        if not filesize == 0:
            result_file = open(resultfile, "a+")
            result_file.write("orifile: " + orifile + "\n")
            result_file.write("newfile: " + newfile + "\n")
        
    def _get_current_time(self):
        return time.strftime('%Y-%m-%d-%X',time.localtime(time.time()))

    def write_html_to_file(self,folder, filename, html):
        result_folder = self.currentpath + os.path.sep + folder
        print result_folder
        if not os.path.exists(result_folder):
            os.mkdir(result_folder)
        soup = BeautifulSoup(html)
        html = soup.prettify()
        p = re.compile("style=.*\"")
        html = p.sub("",html)
        p = re.compile("transform=.*\"")
        html = p.sub("",html)
        print html
        result_file_path = self.currentpath + os.path.sep + folder + os.path.sep + filename
        result_file = open(result_file_path, "w")
        result_file.write(html)

    def compare_file_source(self,orifolder, orifile_name,newfolder, newfile_name, result_file):
        result_folder_path = self.currentpath + os.path.sep + self.result_folder
        resultfilepath = result_folder_path + os.path.sep + result_file
        orifolder_path = self.currentpath + os.path.sep + orifolder
        newfolder_path = self.currentpath + os.path.sep + newfolder
        if not os.path.exists(result_folder_path):
            os.mkdir(result_folder_path)
        if not os.path.exists(orifolder_path):
            print "Base HTML folder dose not exist."
            all_results_file = open(self.currentpath + os.path.sep + self.all_comparation_results,"a+")
            current_time = self._get_current_time()
            all_results_file.write("time:" + current_time + \
                       ": there is no base folder " + orifolder + ".\n")
            return
        if not os.path.exists(newfolder_path):
            os.mkdir(newfolder_path)  
        orifilepath = orifolder_path + os.path.sep + orifile_name
        newfilepath = newfolder_path + os.path.sep + newfile_name
        if not os.path.exists(orifilepath):
            print "Orifilepath ", orifile_name, " does not exist."
            all_results_file = open(self.currentpath + os.path.sep \
                                    + self.all_comparation_results,"a+")
            current_time = self._get_current_time()
            all_results_file.write("time:" + current_time + \
                                   ": there is no base file " + \
                                   orifile_name + ".\n")
            return
        if not os.path.exists(newfilepath):
            raise IOError
        self._compare_files(orifilepath, newfilepath, resultfilepath)
        self.write_result_to_file(result_file)
        
    def write_result_to_file(self, filename):
        resultfile = self.currentpath + os.path.sep + \
            self.result_folder + os.path.sep + filename
        filesize = os.path.getsize(resultfile)
        currenttime = self._get_current_time()
        print currenttime
        all_results_file = open(self.currentpath + os.path.sep + self.all_comparation_results,"a+")
        if not filesize == 0:
            all_results_file.write("time: " + currenttime + ", case " + filename + " has something different, please check file " + filename + ". \n")
        else:
            all_results_file.write("time: " + currenttime + ", case " + \
                                   filename + " has passed.\n")        
            
    def write_test_suite(self, test_suite):
        result_file_path = self.currentpath + os.path.sep \
        + self.all_comparation_results
        result_file = open(result_file_path, "a+")
        result_file.write("\n**************************" + \
                          test_suite + "**************************\n")
